package model;

import static org.junit.Assert.fail;
import accounts.Authorization;
import accounts.UserAccount;
import model.environmentConfig.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.*;

import view.Mode;

public class EnvironmentManager {

	private EnvironmentConfiguration envConfig;
	private UserAccount uA;
	private Mode mode;
	
	public EnvironmentManager(){
		envConfig = new EnvironmentConfiguration();
		uA = new UserAccount();
		mode = Mode.NONE;
	}
	
	public EnvironmentConfiguration getEnvironmentConfiguration(){
		return envConfig;
	}
	
	public void setEnvironmentConfiguration(EnvironmentConfiguration eC){
		envConfig = eC;
	}
	
	public void setAuthorization(Authorization a){
		uA.setAuthorization(a);
	}
	
	public Authorization getAuthorization(){
		return uA.getAuthorization();
	}
	
	public Mode getMode(){
		return this.mode;
	}
	
	public void setMode(Mode m){
		this.mode = m;
	}
	
	public void newEnvironment(){
		envConfig = new EnvironmentConfiguration();
	}
	
	public void loadEnvironment(String XMLFile){
		EnvironmentConfiguration eC =null;
		try{
			eC = XMLInterface.loadXMLFile(XMLFile);
			String xmlFile = XMLFile.split("\\.")[1];
			xmlFile = xmlFile.split("\\.")[0];
			xmlFile = (xmlFile.split("/")[2]);
			//files.add(xmlFile);
			eC.setFileName(xmlFile);
		}
		catch(Exception e){
			System.out.println(e.getLocalizedMessage());
			System.out.println(e.getMessage());
			fail("Something went wrong");
			eC = null;
		}
		envConfig = eC;
	}
	
	public void saveEnvironment(String XMLFile){
		//EnvironmentConfiguration eC =null;
		try{
			XMLInterface.saveXMLFile(XMLFile, envConfig);
		}
		catch(Exception e){
			System.out.println(e.getLocalizedMessage());
			System.out.println(e.getMessage());
			fail("Something went wrong");
			//eC = null;
		}
		
	}
	
	public void dumpToFile(String content, String fileName)
	{
		
	}
	
	public void deleteEnvironment(String XMLFilename)
	{
		File file = new File(XMLFilename);
		file.delete();
	}
	
	//TODO: remove this function in .vpp and here
	public void modifyEnvironment(String XMLFilename)
	{
		
	}
	
	public void copyEnvironment(String XMLFilename, String NewXMLFilename)
	{
		
		InputStream inStream = null;
		OutputStream outStream = null;
		try{
			
				File file = new File(XMLFilename);
				File copyFile = new File("./xmls/" + NewXMLFilename + ".xml");
		
				inStream = new FileInputStream(file);
	    	    outStream = new FileOutputStream(copyFile);
	 
	    	    byte[] buffer = new byte[1024];
	 
	    	    int length;
	    	    //copy the file content in bytes 
	    	    while ((length = inStream.read(buffer)) > 0){
	 
	    	    	outStream.write(buffer, 0, length);
	 
	    	    }
	 
	    	    inStream.close();
	    	    outStream.close();
	 
	    	    System.out.println("File was copied successfully!");
		}
	
		catch(IOException e){
    		e.printStackTrace();
    	}
		//copyFile = file;
		//copyFile.copy(XMLFilename,"./xmls/" + NewXMLFilename,1);
		
	}
	
	//designNode()
	//designChannel()
	
	public void submitForAproval(String XMLFilename)
	{
		
	}
	
	public EnvironmentConfiguration createEnvironment()
	{
		EnvironmentConfiguration newEnvironment = null;
		return newEnvironment;
	}
	
		
	public void generateEnvDoc(String XMLFilename, String Reportname)
	{
		InputStream inStream = null;
		OutputStream outStream = null;
		String content = "Report";
		int nodeCounter = 1;
		int channelCounter = 1;
		
		try{
			
				File file = new File(XMLFilename);
				File reportFile = new File("./reports/" + Reportname + ".txt");
		
				inStream = new FileInputStream(file);
	    	    outStream = new FileOutputStream(reportFile);
	    	   	 
	    	    byte[] buffer = new byte[1024];
	    	    byte[] contentInBytes = content.getBytes();
	 
	    	    int length;
	    	    //copy the file content in bytes 
	    	    //while ((length = inStream.read(buffer)) > 0){
	 
	    	    FileInputStream fstream = new FileInputStream(file);
	    	    DataInputStream in = new DataInputStream(fstream);
	    	    BufferedReader br = new BufferedReader(new InputStreamReader(in));
	    	    String strLine;
	    	    PrintWriter out = new PrintWriter(reportFile);
	    	    
	    	    while ((strLine = br.readLine()) != null) {
	    	    
	    	    	if(strLine.contains("<nodes>"))
	    	    	{
	    	    		out.println("Beginning of Node List");
	    	    	}
	    	    	
	    	    	if(strLine.contains("<key>"))
	    	    	{
	    	    		out.println();
	    	    		out.println("Node " + nodeCounter++);
	    	    	}
	    	    	
	    	    	if(strLine.contains("<height>"))
	    	    	{
	    	    		
	    	    		out.print("Height: ");
	    	    		
	    	    		length = strLine.length();
	    	    		char[] dataCharArray = strLine.toCharArray();
	 
	    	    		for(int x = 0; x<length; x++)
	    	    		{
	    	    			if(dataCharArray[x] == '>' && x+1 != length)
	    	    			{
	    	    				while(dataCharArray[x] != '<')
	    	    				{
	    	    					x++;
	    	    					if(dataCharArray[x] != '<')
	    	    					{
	    	    						out.print(dataCharArray[x]);
	    	    					}//end inner if
	    	    				}//end while loop
	    	    			}//end outer if
	    	    			
	    	    		}//end for loop
	    	    		
	    	    		out.println();
	    	    		
	    	    	}
	    	    	
	    	    	if(strLine.contains("<width>"))
	    	    	{
	    	    		out.print("Width: ");
	    	    		
	    	    		length = strLine.length();
	    	    		char[] dataCharArray = strLine.toCharArray();
	 
	    	    		for(int x = 0; x<length; x++)
	    	    		{
	    	    			if(dataCharArray[x] == '>' && x+1 != length)
	    	    			{
	    	    				while(dataCharArray[x] != '<')
	    	    				{
	    	    					x++;
	    	    					if(dataCharArray[x] != '<')
	    	    					{
	    	    						out.print(dataCharArray[x]);
	    	    					}//end inner if
	    	    				}//end while loop
	    	    			}//end outer if
	    	    			
	    	    		}//end for loop
	    	    		
	    	    		out.println();
	    	    	}
	    	    	
	    	    	if(strLine.contains("<x>"))
	    	    	{
	    	    		out.print("X-Coordinate: ");
	    	    		
	    	    		length = strLine.length();
	    	    		char[] dataCharArray = strLine.toCharArray();
	 
	    	    		for(int x = 0; x<length; x++)
	    	    		{
	    	    			if(dataCharArray[x] == '>' && x+1 != length)
	    	    			{
	    	    				while(dataCharArray[x] != '<')
	    	    				{
	    	    					x++;
	    	    					if(dataCharArray[x] != '<')
	    	    					{
	    	    						out.print(dataCharArray[x]);
	    	    					}//end inner if
	    	    				}//end while loop
	    	    			}//end outer if
	    	    			
	    	    		}//end for loop
	    	    		
	    	    		out.println();
	    	    	}
	    	    	
	    	    	if(strLine.contains("<y>"))
	    	    	{
	    	    		out.print("Y-Coordinate: ");
	    	    		
	    	    		length = strLine.length();
	    	    		char[] dataCharArray = strLine.toCharArray();
	 
	    	    		for(int x = 0; x<length; x++)
	    	    		{
	    	    			if(dataCharArray[x] == '>' && x+1 != length)
	    	    			{
	    	    				while(dataCharArray[x] != '<')
	    	    				{
	    	    					x++;
	    	    					if(dataCharArray[x] != '<')
	    	    					{
	    	    						out.print(dataCharArray[x]);
	    	    					}//end inner if
	    	    				}//end while loop
	    	    			}//end outer if
	    	    			
	    	    		}//end for loop
	    	    		
	    	    		out.println();
	    	    	}
	    	    	
	    	    	if(strLine.contains("<name>"))
	    	    	{
	    	    		out.print("Name: ");
	    	    		
	    	    		length = strLine.length();
	    	    		char[] dataCharArray = strLine.toCharArray();
	 
	    	    		for(int x = 0; x<length; x++)
	    	    		{
	    	    			if(dataCharArray[x] == '>' && x+1 != length)
	    	    			{
	    	    				while(dataCharArray[x] != '<')
	    	    				{
	    	    					x++;
	    	    					if(dataCharArray[x] != '<')
	    	    					{
	    	    						out.print(dataCharArray[x]);
	    	    					}//end inner if
	    	    				}//end while loop
	    	    			}//end outer if
	    	    			
	    	    		}//end for loop
	    	    		
	    	    		out.println();
	    	    	}
	    	    	
	    	    	if(strLine.contains("<operatingSystem>"))
	    	    	{
	    	    		out.print("Operating System: ");
	    	    		
	    	    		length = strLine.length();
	    	    		char[] dataCharArray = strLine.toCharArray();
	 
	    	    		for(int x = 0; x<length; x++)
	    	    		{
	    	    			if(dataCharArray[x] == '>' && x+1 != length)
	    	    			{
	    	    				while(dataCharArray[x] != '<')
	    	    				{
	    	    					x++;
	    	    					if(dataCharArray[x] != '<')
	    	    					{
	    	    						out.print(dataCharArray[x]);
	    	    					}//end inner if
	    	    				}//end while loop
	    	    			}//end outer if
	    	    			
	    	    		}//end for loop
	    	    		
	    	    		out.println();
	    	    	}
	    	    	
	    	    	if(strLine.contains("<externalIPAddress>"))
	    	    	{
	    	    		out.print("External IP Address: ");
	    	    		
	    	    		length = strLine.length();
	    	    		char[] dataCharArray = strLine.toCharArray();
	 
	    	    		for(int x = 0; x<length; x++)
	    	    		{
	    	    			if(dataCharArray[x] == '>' && x+1 != length)
	    	    			{
	    	    				while(dataCharArray[x] != '<')
	    	    				{
	    	    					x++;
	    	    					if(dataCharArray[x] != '<')
	    	    					{
	    	    						out.print(dataCharArray[x]);
	    	    					}//end inner if
	    	    				}//end while loop
	    	    			}//end outer if
	    	    			
	    	    		}//end for loop
	    	    		
	    	    		out.println();
	    	    	}
	    	    	
	    	    	if(strLine.contains("<internalIPAddress>"))
	    	    	{
	    	    		out.print("Internal IP Address: ");
	    	    		
	    	    		length = strLine.length();
	    	    		char[] dataCharArray = strLine.toCharArray();
	 
	    	    		for(int x = 0; x<length; x++)
	    	    		{
	    	    			if(dataCharArray[x] == '>' && x+1 != length)
	    	    			{
	    	    				while(dataCharArray[x] != '<')
	    	    				{
	    	    					x++;
	    	    					if(dataCharArray[x] != '<')
	    	    					{
	    	    						out.print(dataCharArray[x]);
	    	    					}//end inner if
	    	    				}//end while loop
	    	    			}//end outer if
	    	    			
	    	    		}//end for loop
	    	    		
	    	    		out.println();
	    	    	}
	    	    	
	    	    	if(strLine.contains("</nodes>"))
	    	    	{
	    	    		out.println("End of Node List");
	    	    	}
	    	    	
	    	    	if(strLine.contains("<channels>"))
	    	    	{
	    	    		out.println();
	    	    		out.println("Beginning of Channel List");
	    	    	}
	    	    	
	    	    	if(strLine.contains("<channel>"))
	    	    	{
	    	    		out.println();
	    	    		out.println("Channel " + channelCounter++);
	    	    	}
	    	    	
	    	    	if(strLine.contains("<initiatorNode>"))
	    	    	{
	    	    		out.print("Intiator Node: ");
	    	    		
	    	    		length = strLine.length();
	    	    		char[] dataCharArray = strLine.toCharArray();
	 
	    	    		for(int x = 0; x<length; x++)
	    	    		{
	    	    			if(dataCharArray[x] == '>' && x+1 != length)
	    	    			{
	    	    				while(dataCharArray[x] != '<')
	    	    				{
	    	    					x++;
	    	    					if(dataCharArray[x] != '<')
	    	    					{
	    	    						out.print(dataCharArray[x]);
	    	    					}//end inner if
	    	    				}//end while loop
	    	    			}//end outer if
	    	    			
	    	    		}//end for loop
	    	    		
	    	    		out.println();
	    	    	}
	    	    	
	    	    	if(strLine.contains("<listeningNode>"))
	    	    	{
	    	    		out.print("Listening Node: ");
	    	    		
	    	    		length = strLine.length();
	    	    		char[] dataCharArray = strLine.toCharArray();
	 
	    	    		for(int x = 0; x<length; x++)
	    	    		{
	    	    			if(dataCharArray[x] == '>' && x+1 != length)
	    	    			{
	    	    				while(dataCharArray[x] != '<')
	    	    				{
	    	    					x++;
	    	    					if(dataCharArray[x] != '<')
	    	    					{
	    	    						out.print(dataCharArray[x]);
	    	    					}//end inner if
	    	    				}//end while loop
	    	    			}//end outer if
	    	    			
	    	    		}//end for loop
	    	    		
	    	    		out.println();
	    	    	}
	    	    	
	    	    	if(strLine.contains("<initiatorPortRangeStart>"))
	    	    	{
	    	    		out.print("Initiator Port Range Start: ");
	    	    		
	    	    		length = strLine.length();
	    	    		char[] dataCharArray = strLine.toCharArray();
	 
	    	    		for(int x = 0; x<length; x++)
	    	    		{
	    	    			if(dataCharArray[x] == '>' && x+1 != length)
	    	    			{
	    	    				while(dataCharArray[x] != '<')
	    	    				{
	    	    					x++;
	    	    					if(dataCharArray[x] != '<')
	    	    					{
	    	    						out.print(dataCharArray[x]);
	    	    					}//end inner if
	    	    				}//end while loop
	    	    			}//end outer if
	    	    			
	    	    		}//end for loop
	    	    		
	    	    		out.println();
	    	    	}
	    	    	
	    	    	if(strLine.contains("<initiatorPortRangeEnd>"))
	    	    	{
	    	    		out.print("Initiator Port Range End: ");
	    	    		
	    	    		length = strLine.length();
	    	    		char[] dataCharArray = strLine.toCharArray();
	 
	    	    		for(int x = 0; x<length; x++)
	    	    		{
	    	    			if(dataCharArray[x] == '>' && x+1 != length)
	    	    			{
	    	    				while(dataCharArray[x] != '<')
	    	    				{
	    	    					x++;
	    	    					if(dataCharArray[x] != '<')
	    	    					{
	    	    						out.print(dataCharArray[x]);
	    	    					}//end inner if
	    	    				}//end while loop
	    	    			}//end outer if
	    	    			
	    	    		}//end for loop
	    	    		
	    	    		out.println();
	    	    	}
	    	    	
	    	    	if(strLine.contains("<listenerPortRangeStart>"))
	    	    	{
	    	    		out.print("Listener Port Range Start: ");
	    	    		
	    	    		length = strLine.length();
	    	    		char[] dataCharArray = strLine.toCharArray();
	 
	    	    		for(int x = 0; x<length; x++)
	    	    		{
	    	    			if(dataCharArray[x] == '>' && x+1 != length)
	    	    			{
	    	    				while(dataCharArray[x] != '<')
	    	    				{
	    	    					x++;
	    	    					if(dataCharArray[x] != '<')
	    	    					{
	    	    						out.print(dataCharArray[x]);
	    	    					}//end inner if
	    	    				}//end while loop
	    	    			}//end outer if
	    	    			
	    	    		}//end for loop
	    	    		
	    	    		out.println();
	    	    	}
	    	    	
	    	    	if(strLine.contains("<listenerPortRangeEnd>"))
	    	    	{
	    	    		out.print("Listener Port Range End: ");
	    	    		
	    	    		length = strLine.length();
	    	    		char[] dataCharArray = strLine.toCharArray();
	 
	    	    		for(int x = 0; x<length; x++)
	    	    		{
	    	    			if(dataCharArray[x] == '>' && x+1 != length)
	    	    			{
	    	    				while(dataCharArray[x] != '<')
	    	    				{
	    	    					x++;
	    	    					if(dataCharArray[x] != '<')
	    	    					{
	    	    						out.print(dataCharArray[x]);
	    	    					}//end inner if
	    	    				}//end while loop
	    	    			}//end outer if
	    	    			
	    	    		}//end for loop
	    	    		
	    	    		out.println();
	    	    	}
	    	    	
	    	    	if(strLine.contains("</channels>"))
	    	    	{
	    	    		out.println("End of Channel List");
	    	    	}
	    	    }

	    	    in.close();
	    	    out.close();	
	    	    //outStream.write(contentInBytes);
	    	     	
	    	    		 
	    	    //}
	 
	    	    inStream.close();
	    	    outStream.close();
	 
	    	    System.out.println("Report generated successfully!");
		}
	
		catch(IOException e){
    		e.printStackTrace();
    	}
	}

	public void addNode(Node node) {
		envConfig.addNode(node);
		
	}
	
}
